#!/usr/bin/env python
# -*- coding: utf-8 -*-
#在本地存储每个一级内容的更新时间频度，通过回复设定每个用户的每个内容类别的更新频度
#，但是只是默认值从服务器取，每个用户的更新频度存储在本地
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from DeleteOldFile import deleteOldImage, deleteOldContent
from loginTool import Login, Logout, login_required

from photoLib import *
from userPhotoLib import *
from photo import *
from userInfo import *
from content import *
from subscribe import *
from reply import *
from control import *


from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

#class Photo(db.Model): #//图片表
  #author = db.UserProperty()#//无效去除
#  photoCode = db.IntegerProperty()#//图片编码
#  photoFile = db.StringProperty(multiline=True)#//图片文件
  #photoFile_Small = db.StringProperty(multiline=True)#//图片缩略图片
#  photoFile_Small = db.BlobProperty()
#  date = db.DateTimeProperty(auto_now_add=True)#//图片生成时间

class PublicPage(webapp.RequestHandler):
  def render(self, template_file, template_value):
      path = os.path.join(os.path.dirname(__file__), template_file)
      self.response.out.write(template.render(path, template_value))

class MainPage(PublicPage):
  def get(self):
      url = users.create_logout_url(self.request.uri)
      template_values = {'url':url,}
      self.render('template/login.html',template_values)

class LoginWord(PublicPage):
  def get(self):
      self.render('template/workframe.html',{})
  def post(self):
      self.render('template/workframe.html',{})
#      userName = self.request.get('username')
#      passWord = self.request.get('password')
      #清理数据才使用
     # cs = db.GqlQuery("SELECT * FROM ContentClass")
     # for s in cs:
     #   db.delete(s)
     # cs = db.GqlQuery("SELECT * FROM ReplyContent")
     # for s in cs:
     #   db.delete(s)
     # cs = db.GqlQuery("SELECT * FROM Subscribe")
     # for s in cs:
     #   db.delete(s)
     # cs = db.GqlQuery("SELECT * FROM SubscribeOther")
     # for s in cs:
     #   db.delete(s)
#      if userName == 'admin' and passWord == '123456':
##         self.redirect('/index?userName=%s' %userName)
#         self.render('template/workframe.html',{})
#      else:
#         self.response.out.write('<div><script type="text/javascript">alert("管理员名字或密码错误！"); </script></div>')
#         self.render('template/login.html',{})
         
#class Register(PublicPage):
#  def get(self):
#      url = self.request.url
#      template_values = {'url':url,}
#      self.render('register.html',template_values)

class ExitSystem(PublicPage):
  @login_required
  def get(self):
      self.redirect(users.create_logout_url('/'))
#      self.render('template/login.html',{})
class Top(PublicPage):
  @login_required
  def get(self):
      self.render('template/topnav.html',{})
class Menu(PublicPage):
  @login_required
  def get(self):
      self.render('template/menu.html',{})

application = webapp.WSGIApplication([
  ('/', MainPage),
#  ('/loginwork',LoginWord),
  #('/register',Register),
  ('/top',Top),
  ('/menu',Menu),
  ('/exitSystem',ExitSystem),
  ('/photoLibList',PhotoLibList),
  ('/photoLibAdd',PhotoLibAdd),
  ('/photoLibDelete',PhotoLibDelete),
  ('/searchPhoto',SearchPhoto),
  ('/addPhotoToLib',AddPhotoToLib),
  ('/photoLib_PhotoList',PhotoLib_PhotoList),
  ('/photoLib_PhotoDelete',PhotoLib_PhotoDelete),
  ('/photoLib_PhotoUpdate',PhotoLib_PhotoUpdate),
  ('/user_PhotoLibList',User_PhotoLibList),
  ('/user_PhotoLibAdd',User_PhotoLibAdd),
  ('/user_PhotoLibDelete',User_PhotoLibDelete),
  ('/user_PhotoLib_PhotoList',User_PhotoLib_PhotoList),
  ('/photoList/(?P<page>[0-9]*)', PhotoList),
  ('/photoInfoList/(?P<page>[0-9]*)', PhotoInfoList),
  ('/photoInfo/(?P<page>[0-9]*)', PhotoInfo),
  ('/show/(?P<id>[0-9]+)',ShowImage),
  ('/photoUpload', photoUpload),
  ('/(images)/([0-9]+)',GetImage),

  ('/admin/delid/(?P<id>[0-9]+)', Delete_Image_ID),
  ('/admin/delInfo/(?P<id>[0-9]+)', DeletePhotoInfo),
  ('/admin/update', PhotoInfoUpdate),
  ('/userList/(?P<page>[0-9]*)',UserList),
  ('/userInfo',UserInfo),
  ('/userAdd',UserAdd),
  ('/userDelete',UserDelete),
  ('/userUpdate',UserUpdate),
  ('/userPassWordUpdate',UserPassWordUpdate),
  ('/contentList/(?P<page>[0-9]*)',ContentList),
  ('/contentAdd',ContentAdd),
  ('/contentDelete',ContentDelete),
  ('/contentUpdate',ContentUpdate),
  ('/replyContentList/(?P<page>[0-9]*)',ReplyContentList),
  ('/replyContentAdd', ReplyContentAdd),
  ('/replyContentDelete', ReplyContentDelete),
  ('/subscribeInfo', SubscribeInfo),
  ('/subscribeList', SubscribeList),
  ('/subscribeAdd', SubscribeAdd),
  ('/userSubscribeAdd', UserSubscribeAdd),
  ('/subscribeDelete',SubscribeDelete),
  ('/subscribeStatus',SubscribeStatus),
  ('/subscribeOtherAdd', SubscribeOtherAdd),
  ('/subscribeOtherList', SubscribeOtherList),
  ('/subscribeOtherDelete', SubscribeOtherDelete),
  ('/userReplyList/(?P<page>[0-9]*)',UserReplyList),
  ('/userReplyAdd',UserReplyAdd),
  ('/userReplyDelete',UserReplyDelete),
  ('/userReplyUpdate',UserReplyUpdate),
  ###定时删除文件##
  ('/deleteOldImage',deleteOldImage),
  ('/deleteOldContent',deleteOldContent),
  ###login logout##
  ('/login',Login),
  ('/logout',Logout),
#  ('/UserLogin', UserLogin),
#  ('/UserRegister', UserRegister),
#  ('/AddReply', AddReply),
#  ('/PhotoAll',PhotoLibAll),
#  ('/PhotoUpdate', PhotoUpdate),
#  ('/InfoAll',InfoAll),
#  ('/InfoUpdate', InfoUpdate),
], debug=True)


def main():
    run_wsgi_app(application)
  #wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    try:
        main()
    except Exception,e:
        logging.error(str(e))
